package com.zhongjian.technologycal.mall.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.LoginActivity;
import com.zhongjian.technologycal.mall.activity.personal.SettingsActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.ApplyPartnerActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.FansAccountActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.MallOrdersListActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.MallPresenterActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.MineCodeCardActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.MineCourseActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.MineMallWalletActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.PersonalCenterInfoActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.RealNameActivity;
import com.zhongjian.technologycal.mall.activity.personal.activity.ServiceCenterActivity;
import com.zhongjian.technologycal.mall.activity.web.WebViewMarkActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.base.BaseFragment;
import com.zhongjian.technologycal.mall.bean.UserInfoBean;
import com.zhongjian.technologycal.mall.dialog.PromptDialog;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingTip;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.GetDialog;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import me.drakeet.materialdialog.MaterialDialog;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/10/16.
 * <b>个人中心页面</b>≈
 */

public class PersonalCenterFragment extends BaseFragment {
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.pesonal_iv_heading)
    CircleImageView pesonal_iv_heading;
    @BindView(R.id.my_order_imageview)
    ImageView badgeImageView;
    @BindView(R.id.personal_tv_nickname)
    TextView personal_tv_nickname;
    @BindView(R.id.tv_cash_money)
    TextView tv_cash_money;//现金币
    @BindView(R.id.tv_shopping_currency)
    TextView tv_shopping_currency;//购物币
    @BindView(R.id.tv_consumption_points)
    TextView tv_consumption_points;//消费积分
    @BindView(R.id.order_tv_amount0)
    TextView order_tv_amount0;
    @BindView(R.id.order_tv_amount1)
    TextView order_tv_amount1;
    @BindView(R.id.order_tv_amount2)
    TextView order_tv_amount2;
    @BindView(R.id.order_tv_amount3)
    TextView order_tv_amount3;
    @BindView(R.id.tv_user_id)
    TextView tv_user_id;
    @BindView(R.id.loadedTip)
    LoadingTip loadedTip;
    private String username;
    @BindView(R.id.apply_ll_proxy)
    LinearLayout apply_ll_proxy;
    @BindView(R.id.apply_img_status)
    TextView apply_img_status;
    @BindView(R.id.level_tv_status)
    TextView level_tv_status;
    private Dialog hintDialog = null;
    private GetDialog getDialog = new GetDialog();
    private MaterialDialog mMaterialDialog = null;

    @Override
    protected void initView() {
        StatusBarCompat.setStatusBarColor(getActivity(), getResources().getColor(R.color.blue), true);
        refreshLayout.setPrimaryColorsId(R.color.blue);
        refreshLayout.setHeaderMaxDragRate(4);
        refreshLayout.setEnableLoadmore(false);
        if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
            level_tv_status.setVisibility(View.GONE);
            order_tv_amount0.setVisibility(View.GONE);
            order_tv_amount1.setVisibility(View.GONE);
            order_tv_amount2.setVisibility(View.GONE);
            order_tv_amount3.setVisibility(View.GONE);
            personal_tv_nickname.setText("未登录,请先登录账号");
            tv_user_id.setVisibility(View.GONE);
            tv_cash_money.setText("0.0");
            tv_shopping_currency.setText("0.0");
            tv_consumption_points.setText("0.0");

        } else {
            initRequest();
        }

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initRequest();
                        refreshlayout.finishRefresh();
                        refreshlayout.setLoadmoreFinished(false);
                    }
                }, 1000);
            }
        });


    }

    @Override
    public void initPresenter() {

    }

    /**
     * @return
     */
    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_personal_center;
    }


    /**
     * 我的钱包
     */
    @OnClick(R.id.layout_ll_my_wallet)
    public void onClickMyWallet() {


    }

    /**
     * @param view
     */
    @OnClick({R.id.layout_cash_money, R.id.layout_shopping_currency, R.id.layout_consumption_points, R.id.iv_setting,
            R.id.person_ll_my_order, R.id.person_ll_wait_payment, R.id.person_ll_wait_send, R.id.person_ll_wait_bleed,
            R.id.person_ll_wait_appraise, R.id.layout_my_fans, R.id.layout_my_footprint, R.id.layout_my_customer,
            R.id.layout_always_problem, R.id.pesonal_iv_heading, R.id.layout_my_card, R.id.apply_ll_proxy,
            R.id.pensonal_layout_my_card, R.id.layout_my_course})
    public void onClickViewListeners(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            /**
             * 现金币
             */
            case R.id.layout_cash_money:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MineMallWalletActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 购物币
             */
            case R.id.layout_shopping_currency:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MineMallWalletActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 消费积分
             */
            case R.id.layout_consumption_points:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MineMallWalletActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 设置
             */
            case R.id.iv_setting:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), SettingsActivity.class);
                    intent.putExtra("username", username);
                    startActivity(intent);
                }
                break;
            /**
             * 我的订单
             */
            case R.id.person_ll_my_order:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MallOrdersListActivity.class);
                    intent.putExtra("tabState", 0);
                    startActivity(intent);
                }
                break;
            /**
             * 待付款
             */
            case R.id.person_ll_wait_payment:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MallOrdersListActivity.class);
                    intent.putExtra("tabState", 1);
                    startActivity(intent);
                }
                break;
            /**
             * 待发货
             */
            case R.id.person_ll_wait_send:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MallOrdersListActivity.class);
                    intent.putExtra("tabState", 2);
                    startActivity(intent);
                }
                break;
            /**
             * 待收货
             */
            case R.id.person_ll_wait_bleed:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MallOrdersListActivity.class);
                    intent.putExtra("tabState", 3);
                    startActivity(intent);
                }
                break;
            /**
             * 待评价
             */
            case R.id.person_ll_wait_appraise:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MallOrdersListActivity.class);
                    intent.putExtra("tabState", 4);
                    startActivity(intent);
                }
                break;
            /**
             * 我的粉丝
             */
            case R.id.layout_my_fans:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), FansAccountActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 二送一
             */
            case R.id.layout_my_footprint:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MallPresenterActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 客服中心
             */
            case R.id.layout_my_customer:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), ServiceCenterActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 实名认证
             */
            case R.id.layout_always_problem:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), RealNameActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 个人中心
             */
            case R.id.pesonal_iv_heading:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), PersonalCenterInfoActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 我的名片
             */
            case R.id.layout_my_card:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MineCodeCardActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 申请代理
             */
            case R.id.apply_ll_proxy:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), ApplyPartnerActivity.class);
                    startActivity(intent);
                }
                break;
            /**
             * 水机兑换
             */
            case R.id.pensonal_layout_my_card:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), WebViewMarkActivity.class);
                    intent.putExtra("url", BaseContacts.URL_CARD + UserUtils.getUserToken());
                    startActivity(intent);
                }
                break;
            /**
             * 我的课程
             */
            case R.id.layout_my_course:
                if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
                    intent.setClass(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent.setClass(getActivity(), MineCourseActivity.class);
                    intent.putExtra("tabState", 0);
                    startActivity(intent);
                }

                break;

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 获取个人中心数据
     */
    private void initRequest() {
        if (!NetWorkUtils.isNetConnected(getActivity())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getActivity()).getUrlRequestCenterData(UserUtils.getUserToken(),
                new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            Gson gson = new Gson();
                            UserInfoBean userInfoBean = gson.fromJson(responseBody.string(), UserInfoBean.class);
                            if (userInfoBean.getError_code() == 0) {
                                tv_user_id.setVisibility(View.VISIBLE);
                                tv_user_id.setText("ID: " + userInfoBean.getData().getPersonDataMap().getSysID());
                                tv_cash_money.setText("" + userInfoBean.getData().getPersonDataMap().getRemainElecNum());
                                tv_shopping_currency.setText("" + userInfoBean.getData().getPersonDataMap().getRemainPoints());
                                tv_consumption_points.setText("" + userInfoBean.getData().getPersonDataMap().getRemainVIPAmount());

                                if (userInfoBean.getData().getOrderStatusList().get(0) == 0) {
                                    order_tv_amount0.setVisibility(View.GONE);
                                } else {
                                    order_tv_amount0.setText("" + userInfoBean.getData().getOrderStatusList().get(0));
                                    order_tv_amount0.setVisibility(View.VISIBLE);
                                }
                                if (userInfoBean.getData().getOrderStatusList().get(1) == 0) {
                                    order_tv_amount1.setVisibility(View.GONE);
                                } else {
                                    order_tv_amount1.setText("" + userInfoBean.getData().getOrderStatusList().get(1));
                                    order_tv_amount1.setVisibility(View.VISIBLE);
                                }
                                if (userInfoBean.getData().getOrderStatusList().get(2) == 0) {
                                    order_tv_amount2.setVisibility(View.GONE);
                                } else {
                                    order_tv_amount2.setText("" + userInfoBean.getData().getOrderStatusList().get(2));
                                    order_tv_amount2.setVisibility(View.VISIBLE);
                                }
                                if (userInfoBean.getData().getOrderStatusList().get(3) == 0) {
                                    order_tv_amount3.setVisibility(View.GONE);
                                } else {
                                    order_tv_amount3.setText("" + userInfoBean.getData().getOrderStatusList().get(3));
                                    order_tv_amount3.setVisibility(View.VISIBLE);
                                }

                                personal_tv_nickname.setText(userInfoBean.getData().getPersonDataMap().getNickName());
//
//                                final RequestOptions options = new RequestOptions();
//                                options.fitCenter()
//                                        .transform(new GlideRoundTransform(getActivity(), 5))
//                                        .placeholder(R.mipmap.bg_white2x)
//                                        .error(R.mipmap.bg_white2x);
//                                Glide.with(getActivity())
//                                        .load(BaseContacts.BASE_URL + userInfoBean.getData()
//                                                .getPersonDataMap().getHeadPhoto())
//                                        .apply(options)
//                                        .thumbnail(0.1f)
//                                        .into(pesonal_iv_heading);

                                Picasso.get().load(BaseContacts.BASE_URL + userInfoBean.getData()
                                        .getPersonDataMap().getHeadPhoto())
                                        .resize(200,250).into(pesonal_iv_heading);

                                Log.i("photo_url", BaseContacts.BASE_URL + userInfoBean.getData()
                                        .getPersonDataMap().getHeadPhoto());
                                username = userInfoBean.getData().getPersonDataMap().getUserName();
                                if (userInfoBean.getData().getPersonDataMap().getLev() == 0 &&
                                        userInfoBean.getData().isGCmember() == false) {
                                    level_tv_status.setText("免费会员");
                                    level_tv_status.setVisibility(View.VISIBLE);
                                } else if (userInfoBean.getData().getPersonDataMap().getLev() == 0
                                        && userInfoBean.getData().isGCmember() == true) {
                                    level_tv_status.setText("99会员");
                                    level_tv_status.setVisibility(View.VISIBLE);
                                } else if (userInfoBean.getData().getPersonDataMap().getLev() == 1) {
                                    level_tv_status.setText("VIP");
                                    level_tv_status.setVisibility(View.VISIBLE);
                                } else if (userInfoBean.getData().getPersonDataMap().getLev() == 2 &&
                                        userInfoBean.getData().getPersonDataMap().getIsSubProxy() == 0) {
                                    level_tv_status.setText("VIP");
                                    level_tv_status.setVisibility(View.VISIBLE);
                                } else if (userInfoBean.getData().getPersonDataMap().getLev() == 2 &&
                                        userInfoBean.getData().getPersonDataMap().getIsSubProxy() == 1) {
                                    level_tv_status.setText("准代");
                                    level_tv_status.setVisibility(View.VISIBLE);
                                } else if (userInfoBean.getData().getPersonDataMap().getLev() == 3) {
                                    level_tv_status.setText("代理");
                                    level_tv_status.setVisibility(View.VISIBLE);
                                }
                                if (userInfoBean.getData().getProxyApply().getCanProxyApply() == 0) {
                                    apply_ll_proxy.setVisibility(View.INVISIBLE);
                                } else if (userInfoBean.getData().getProxyApply().getCanProxyApply() == 1) {
                                    apply_ll_proxy.setVisibility(View.VISIBLE);
                                    if (userInfoBean.getData().getProxyApply().getIsAlreadyApply() == 1) {
                                        apply_img_status.setVisibility(View.GONE);
                                    } else if (userInfoBean.getData().getProxyApply().getIsAlreadyApply() == 0) {
                                        apply_img_status.setVisibility(View.VISIBLE);
                                    }
                                }

                            } else if (userInfoBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                getActivity().sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort(userInfoBean.getError_message());
                            }


                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });


    }

    @Override
    public void onResume() {
        super.onResume();
        if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
            level_tv_status.setVisibility(View.GONE);
            order_tv_amount0.setVisibility(View.GONE);
            order_tv_amount1.setVisibility(View.GONE);
            order_tv_amount2.setVisibility(View.GONE);
            order_tv_amount3.setVisibility(View.GONE);
            personal_tv_nickname.setText("未登录,请先登录账号");
            tv_user_id.setVisibility(View.GONE);
            tv_cash_money.setText("0.0");
            tv_shopping_currency.setText("0.0");
            tv_consumption_points.setText("0.0");
        } else {
            initRequest();
        }

    }

    @OnClick(R.id.personal_tv_nickname)
    public void onClickLoginNickname() {
        Intent intent = new Intent();
        if (UserUtils.getUserToken() == null || UserUtils.getUserToken().equals("")) {
            intent.setClass(getActivity(), LoginActivity.class);
            startActivity(intent);
        } else {
            intent.setClass(getActivity(), PersonalCenterInfoActivity.class);
            startActivity(intent);
        }
    }

    //商品
    @OnClick(R.id.person_ll_shopping)
    public void onClickShopping() {
//        Intent intent = new Intent();
//        intent.setClass(getActivity(), CommentActivity.class);
//        startActivity(intent);
        new PromptDialog(getActivity())
                .setDialogType(PromptDialog.DIALOG_TYPE_WRONG)
                .setAnimationEnable(true)
                .setContentText("敬请期待...")
                .setPositiveListener("确定", new PromptDialog.OnPositiveListener() {
                    @Override
                    public void onClick(PromptDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();

    }

    //文章
    @OnClick(R.id.person_ll_article)
    public void onClickArticle() {
//        startActivity(HttpTestDemo.class);
        new PromptDialog(getActivity())
                .setDialogType(PromptDialog.DIALOG_TYPE_WRONG)
                .setAnimationEnable(true)
                .setContentText("敬请期待...")
                .setPositiveListener("确定", new PromptDialog.OnPositiveListener() {
                    @Override
                    public void onClick(PromptDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();
    }

    //课程
    @OnClick(R.id.person_ll_course)
    public void onClickCourse() {
        new PromptDialog(getActivity())
                .setDialogType(PromptDialog.DIALOG_TYPE_WRONG)
                .setAnimationEnable(true)
                .setContentText("敬请期待...")
                .setPositiveListener("确定", new PromptDialog.OnPositiveListener() {
                    @Override
                    public void onClick(PromptDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();

    }

    //评价
    @OnClick(R.id.person_ll_appraise)
    public void onClickAppraise() {
//        Intent intent = new Intent();
//        intent.setClass(getActivity(), ShuntLimitActivity.class);
//        startActivity(intent);
        new PromptDialog(getActivity())
                .setDialogType(PromptDialog.DIALOG_TYPE_WRONG)
                .setAnimationEnable(true)
                .setContentText("敬请期待...")
                .setPositiveListener("确定", new PromptDialog.OnPositiveListener() {
                    @Override
                    public void onClick(PromptDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();

    }

    //收藏
    @OnClick(R.id.person_ll_my_college)
    public void onClickCollege() {
        new PromptDialog(getActivity())
                .setDialogType(PromptDialog.DIALOG_TYPE_WRONG)
                .setAnimationEnable(true)
                .setContentText("敬请期待...")
                .setPositiveListener("确定", new PromptDialog.OnPositiveListener() {
                    @Override
                    public void onClick(PromptDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();

    }


}
